<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\facade\Db;

/**
 * @mixin \think\Model
 */
class Admin extends Model
{
    //管理员
    public static function find($id)
    {
        $res = Db::name('admin')->alias('a')->leftjoin('admin_role b', 'a.role_id=b.id')->field('a.*,b.name')->where('a.id', $id)->where('a.del', '0')->find();
        return $res;
    }
    //验证权限
    public static function ability($id)
    {
        $res = Db::name('admin')->alias('a')->leftjoin('admin_role b', 'a.role_id=b.id')->field('a.*,b.name,b.ability')->where('a.id', $id)->where('a.del', '0')->find();
        return $res;
    }
    //验证登录
    public static function log($data)
    {
        $find = Db::name('admin')->alias('a')->leftjoin('admin_role b', 'a.role_id=b.id')->field('a.*,b.name')->where('a.username', $data['username'])->where('a.password', $data['password'])->where('a.del', '0')->find();
        return $find;
    }
    //登录日记
    public static function diary($data)
    {
        $res = Db::name('admin_log')->insert($data);
        return $res;
    }
    //登录日记列表
    public static function diarylist($where = "", $limit, $page)
    {
        $count = Db::name('admin_log')->count();
        //计算出从那条开始查询
        $tol = ($page - 1) * $limit;
        $res = Db::name('admin_log')->where($where)->limit($tol, $limit)->select();
        return ['data' => $res, 'count' => $count];
    }
    //管理员列表
    public static function list($where = "", $limit, $page)
    {
        $count = Db::name('admin')->alias('a')->leftjoin('admin_role b', 'a.role_id=b.id')->field('a.*,b.name')->where($where)->where('a.del', '0')->count();
        //计算出从那条开始查询
        $tol = ($page - 1) * $limit;
        // 查询出当前页数显示的数据
        $list = Db::name('admin')->alias('a')->leftjoin('admin_role b', 'a.role_id=b.id')->field('a.*,b.name')->where($where)->where('a.del', '0')->limit($tol, $limit)->select();
        return  ['data' => $list, 'count' => $count];
    }
    //管理员添加
    public static function add($data)
    {
        $data['add_date'] = time();
        $res = Db::name('admin')->insert($data);
        return $res;
    }
    //管理员编辑
    public static function edit($data)
    {
        $data['rep_date'] = time();
        $res = Db::name('admin')->update($data);
        return $res;
    }
    //管理员删除
    public static function del($id)
    {
        if ($id <= '1') {
            return '不能删除哦！';
        }
        $data['rep_date'] = time();
        $res = Db::name('admin')->where('id', $id)->update(['del' => '1']);
        return $res;
    }
}
